<HTML>
  <HEAD>
    <TITLE>threadpool Documentation</TITLE>
    <LINK HREF="threadpool.css" REL="stylesheet" TYPE="text/css">
  </HEAD>
  <BODY BGCOLOR="#FFFFFF">

    <DIV CLASS="qindex">
      <TABLE BORDER="0" WIDTH="100%">
        <TR>

          <TD ALIGN="LEFT">
            <B>threadpool</B>
          </TD>
          <TD ALIGN="RIGHT">
            <A CLASS="qindex" HREF="../index.html">Overview</A> |
            <A CLASS="qindex" HREF="../reference/annotated.html">Reference</A> |
            <A CLASS="qindex" HREF="../tutorial/intro.html">Tutorial</A> |
            <A CLASS="qindex" HREF="../examples/intro.html">Examples</A> |
            <A CLASS="qindex" HREF="../design/intro.html">Design</A>
          </TD>

        </TR>
      </TABLE>
    </DIV>
    <DIV CLASS="qindex">
      <TABLE BORDER="0" WIDTH="100%">
        <TR>
          <TD ALIGN="LEFT">
            <B>Tutorial</B>
          </TD>
          <TD ALIGN="RIGHT">
            <A CLASS="qindex" HREF="intro.html">Quick Start</A> |
            <A CLASS="qindex" HREF="prioritized.html">Prioritized Tasks</A> |
            <A CLASS="qindex" HREF="task_adaptor.html">Arbitrary Task Functions</A> |
            <A CLASS="qindex" HREF="instantiation.html">Advanced Pool Instantiation</A>            
          </TD>
        </TR>
      </TABLE>
    </DIV>
<!-- Generated by Doxygen 1.4.6-NO -->
<h1><a class="anchor" name="prioritized">Prioritized Tasks</a></h1>TODO This tutorial is out dated.<p>
It's easy to prioritize asynchronous tasks by using the task adaptor prio_task_func. The following source listing illustrates how to setup the pool and add the tasks:<p>
<div class="fragment"><pre class="fragment">01 
02  #include <span class="stringliteral">"threadpool.hpp"</span>
03
04  <span class="keyword">using namespace </span>boost::threadpool;
05
06  <span class="comment">// Some example tasks</span>
07  <span class="keywordtype">void</span> normal_task()
08  {
09    ...
10  }
11
13  <span class="keywordtype">void</span> important_task()
14  {
15    ...
16  }
17  
18  <span class="keywordtype">void</span> execute_prioritized()
19  {
20    <span class="comment">// Create prioritized thread pool container without any threads.</span>
21    scoped_pool&lt;prio_pool, 0&gt; tp;
22
23    <span class="comment">// Add some tasks to the pool.</span>
24    tp += prio_task_func(5,   &amp;normal_task);
25    tp += prio_task_func(100, &amp;important_task);
26    tp += prio_task_func(7,   &amp;normal_task);
27
28    <span class="comment">// Add the some threads to the pool. This will start the execution of the tasks.</span>
29    tp-&gt;resize(2);
30
31    <span class="comment">// The tasks are processed according to their priority: important_task(100), nonrelevant_task(7), nonrelevant_task(5).</span>
32
33    tp-&gt;wait();
34    
35    <span class="comment">// Now all tasks are finished and the pool will be destroyed safely when tp goes out of scope.</span>
36  }
37
</pre></div><p>
Like in the first tutorial we start including the main header file and defining some tasks.<p>
At line 21 a prioritized thread pool is created. That means that the pool's tasks are arranged according to their priority before they get executed. Therefore the tasks themselves have to realize a partial ordering based on operator&lt;.<p>
The adaptor prio_thread_func satisfies our requirements regarding the order and is just a small wrapper object for the task functions. In line 24 to 26 some prioritized tasks are scheduled. This time the pool's schedule function is used and like smart pool's += operator this function returns immediately.<p>
At line 29 the first thread is added to the pool and the execution of important_task begins. As we have only one thread the tasks are processed sequentially.<p>
Finally wait() is called to ensure that all tasks are finished before our example function returns and the pool is destroyed. This is very important since the behavior is undefined if pool's lifetime ends while tasks are executed.     <BR>
    <DIV CLASS="qindex">
      <TABLE BORDER="0" WIDTH="100%">
        <TR>
		  <TD CLASS="tiny" ALIGN="LEFT">Copyright &copy 2005-2007 Philipp Henkel</TD>
          <TD ALIGN="RIGHT">
            <A CLASS="qindex" HREF="../index.html">Overview</A> |
            <A CLASS="qindex" HREF="../reference/annotated.html">Reference</A> |
            <A CLASS="qindex" HREF="../tutorial/intro.html">Tutorial</A> |
            <A CLASS="qindex" HREF="../examples/intro.html">Examples</A> |
            <A CLASS="qindex" HREF="../design/intro.html">Design</A>
          </TD>
        </TR>       
      </TABLE>
    </DIV>  
  </BODY>
</HTML>
